package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constant.SystemConstants;
import com.sangeng.constant.enums.AppHttpCodeEnum;
import com.sangeng.domain.core.ResponseResult;
import com.sangeng.domain.dto.RoleInsertDto;
import com.sangeng.domain.dto.RoleListDto;
import com.sangeng.domain.dto.RoleStatusDto;
import com.sangeng.domain.dto.RoleUpdateDto;
import com.sangeng.domain.entity.Menu;
import com.sangeng.domain.entity.RoleMenu;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.domain.vo.RoleVo;
import com.sangeng.domain.vo.UserRoleVo;
import com.sangeng.service.RoleMenuService;
import com.sangeng.utils.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import com.sangeng.domain.entity.Role;
import com.sangeng.mapper.RoleMapper;
import com.sangeng.service.RoleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2025-06-26 16:12:33
 */
@Service("roleService")
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    private final RoleMenuService roleMenuService;
    @Override
    public ResponseResult listAllRoles(RoleListDto roleListDto) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(roleListDto.getRoleName() != null, Role::getRoleName, roleListDto.getRoleName())
                .like(roleListDto.getStatus() != null, Role::getStatus, roleListDto.getStatus())
                .orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(roleListDto.getPageNum(), roleListDto.getPageSize());
        Page<Role> result = page(page, queryWrapper);
        return ResponseResult.okResult(new PageVo(result.getRecords(), result.getTotal()));
    }

    @Override
    public ResponseResult changeStatus(RoleStatusDto roleStatusDto) {
        Role role = new Role();
        role.setId(roleStatusDto.getRoleId());
        role.setStatus(roleStatusDto.getStatus());
        return updateById(role) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Transactional
    @Override
    public ResponseResult insertRole(RoleInsertDto roleInsertDto) {
        Role role = BeanUtil.copyBean(roleInsertDto, Role.class);
        save(role);
        List<RoleMenu> menuIds = roleInsertDto.getMenuIds()
                .stream()
                .map(ids -> new RoleMenu(role.getId(), ids))
                .toList();
        roleMenuService.saveBatch(menuIds);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getRoleById(Long id) {
        Role role = getById(id);
        return ResponseResult.okResult(BeanUtil.copyBean(role, RoleVo.class));
    }

    @Transactional
    @Override
    public ResponseResult updateRole(RoleUpdateDto roleUpdateDto) {
        // 更新角色信息
        Role role = BeanUtil.copyBean(roleUpdateDto, Role.class);
        updateById(role);

        // 删除角色权限表原有信息
        roleMenuService.removeBatchByRoleId(roleUpdateDto.getId());
        // 插入角色权限表新信息
        List<RoleMenu> menuIds = roleUpdateDto.getMenuIds()
                .stream()
                .map(ids -> new RoleMenu(roleUpdateDto.getId(), ids))
                .toList();
        roleMenuService.saveBatch(menuIds);
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult removeRoleById(Long id) {
        // 删除角色信息
        removeById(id);

        // 删除角色权限表信息
        roleMenuService.removeBatchByRoleId(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listAllRole() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, SystemConstants.STATUS_NORMAL);
        List<Role> roles = list(queryWrapper);
        return ResponseResult.okResult(BeanUtil.copyBeanList(roles, UserRoleVo.class));
    }

}

